package com.lognsoft.btest;

import java.util.ArrayList;
import java.util.Properties;
import java.io.File;
import java.lang.String;
import com.lognsoft.shell.*;

class BaseLineTest
{
	Properties   _options = new Properties();
	String       _command = null;
	ArrayList<String> _args = new ArrayList<String>();
  
	static String BTEST_HOME = null;
	static final String BTEST_HOME_KEY = "BTEST_HOME";
	static final String res_name = "com/lognsoft/btest/BaseLineTest.class";


static String get_home_path()
{
	String hm = System.getenv(BTEST_HOME_KEY);

	if (hm != null)
	{
		hm = hm
			.replace('/', File.separatorChar)
			.replace('\\', File.separatorChar);
	  
		if (false == hm.endsWith( File.separator ))
		{
			hm += File.separator;
		}
	  
		return hm;
	}
  
	ClassLoader loader = BaseLineTest.class.getClassLoader();
	String realPath=loader.getResource(res_name).getPath();	  

	if (realPath.startsWith("file:"))
	{
		realPath = realPath.substring(5);
	}
  
	int ind = realPath.indexOf('!');
  
	if (ind < 0)
	{
		realPath = realPath.substring(0, realPath.length() - res_name.length() );
	}
	else
	{
		realPath = realPath.substring(0, ind);
		ind = realPath.lastIndexOf('/');
		realPath = realPath.substring(0, ind + 1);
	}

	try
	{ 
		realPath=java.net.URLDecoder.decode(realPath,"utf-8"); 
	}
	catch(Exception e)
	{
		throw new RuntimeException(e);
	}
  
	if ( realPath.length()  >  3 
			&& realPath.charAt(0) == '/' 
			&& realPath.charAt(2) == ':')
	{
		realPath = realPath.substring(1, realPath.length());
	}
  
	realPath = realPath
		.replace('/',  File.separatorChar)
		.replace('\\', File.separatorChar);
	  
	return realPath;
}
  
public static void main (String[] argv)
{
	BTEST_HOME = get_home_path();
	
	Plugin.plugin.Add("JDBC", JDBC.class);
	Plugin.plugin.Add("ls", ls.class);

	BaseLineTest ot;
	ot = new BaseLineTest();
	
	try
	{
		ot.run (argv);
	}
	catch (TestCaseException te)
	{
		System.err.print(te.getMessage());
	}
	catch (Exception ex)
	{
		ex.printStackTrace();
	}
};

private static void usage ()
{
	String helpString[] = {

"btest version "+Version.FullVersionString+" created by Maomao and Coco",
"",
"usage:",
"   btest [command] [options]",
"",
"commands:",
"   run      : run TestCase",
"   baseline : generate a baseline",
"   view     : view the test cases and test suites",
"   clean    : clean test output files",
"   help     : print this message",
"options",
"   --filter=*          : TestCase Filter",
"   --include=*         : include predefined TestCase",
"   --result=result.xml : test result file",
"",
"function:",
"  btest is an automation test tool, it run the test script in file ",
"  \"TestCase\" at current directory, and compare the standard output",
"  with predefined answer file, The test case is passed when the output",
"  result match with the answer file, otherwise it is failed"
};

	for (int i =0; i< helpString.length; i++)
	{
		System.out.println (helpString[i]);
	};
};

private void parser_args (String[] argv)
{	
	for (String l : argv)
	{	
		if (l.startsWith("--"))
		{
			int ind = l.indexOf('=');
			if ( ind > 0 )
			{
				_options.setProperty(
						l.substring(2,ind),
						l.substring(ind+1).trim()
				);
			}
			else
			{
				_options.setProperty(l.substring(2), "");
			}
		}
		
		else
		{
			if (_command == null)
			{
				_command = l;
			}
			else
			{
				_args.add(l);
			}
		}
	}
	
	if (_command == null)
	{
		_command = "run";
	}
}

private void run (String[] argv) throws Exception
{
	parser_args (argv);
  
	if ("run".equals(_command))
	{
		cmd_run();
		return;
	}
	else if ("baseline".equals(_command))
	{
		cmd_baseline();
	}
	else if ("view".equals(_command))
	{
		cmd_view();
	}
	else if ("clean".equals(_command))
	{
		cmd_clean();
	}
	else if ( "help".equals(_command))
	{
		usage();
	}
	else
	{
		System.out.println("Unkown command " + _command);
	}
};

private void cmd_clean() throws Exception
{
	ExecuteEngine tc = new ExecuteEngine(TestCaseDef.TestCaseFileName);
	
	tc.runClean();
}

private void cmd_run() throws Exception
{
	ExecuteEngine tc = new ExecuteEngine(TestCaseDef.TestCaseFileName);
	
	ExecuteEngine.is_baseline = false;
	ExecuteEngine.report_gen = new ReportGen();
	tc.runTestSuite ();
	
	ExecuteEngine.report_gen.print();
};

private void cmd_baseline() throws Exception
{
	ExecuteEngine tc = new ExecuteEngine(TestCaseDef.TestCaseFileName);
	
	ExecuteEngine.is_baseline = true;
	tc.runTestSuite ();
};

private void cmd_view() throws Exception
{
	ArrayList<ExecuteEngine> TestSuites;
	
	TestSuites = ExecuteEngine.getAll(
			TestCaseDef.TestCaseFileName,
			-1);
	
	
	for (TestCaseFile tc : TestSuites)
	{
		System.out.println ("Test Suite:" + tc.base_path);
		String[] cases = tc.getTestCases(null);
		
		if (cases == null)
		{
			System.out.println("  there is no test case in this test suite");
			System.out.println("");
			continue;
		}
		
		for (String ss : cases)
		{
			System.out.println("  --Cases(" + ss + "):");
		}
		
		System.out.println("");
	}	
};

};